#include "muduo/base/Logging.h"
#include "muduo/base/Mutex.h"
#include "include/muduo/net/TcpClient.h"
#include "include/muduo/net/TcpConnection.h"
#include "include/muduo/net/EventLoopThread.h"
#include "include/muduo/base/CountDownLatch.h"

#include "request.pb.h"

#include "include/codec.h"
#include "include/dispatcher.h"


#include <iostream>
#include <memory>

class Client
{
    typedef std::shared_ptr<request::ResponseTranslate> ResponseTranslatePtr;
    typedef std::shared_ptr<request::ResponseAdd> ResponseAddPtr;

public:
    Client()
    : _latch(1)
    , _dispatcher(std::bind(&Client::defaultMessageCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
    , _codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
    , _client(_loop.startLoop(), muduo::net::InetAddress("127.0.0.1", 8080), "Client")
    {
        _dispatcher.registerMessageCallback<request::ResponseTranslate>(std::bind(&Client::translate, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
        _dispatcher.registerMessageCallback<request::ResponseAdd>(std::bind(&Client::add, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

        _client.setConnectionCallback(std::bind(&Client::onConnection, this, std::placeholders::_1));
        _client.setMessageCallback(std::bind(&ProtobufCodec::onMessage, &_codec, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
    }

    void connect()
    {
        _client.connect();
        _latch.wait();
    }

    void addRequest(int num1, int num2)
    {
        // 定义请求结构化字段
        request::RequestAdd req;
        req.set_num1(num1);
        req.set_num2(num2);

        _codec.send(_conn, req);
    }

    void translateRequest(const std::string& str)
    {
        // 定义请求结构化字段
        request::RequestTranslate req;
        req.set_msg(str);

        _codec.send(_conn, req);
    }


    ~Client() {}

private:

    void onConnection(const muduo::net::TcpConnectionPtr &conn)
    {
        if(conn->connected())
        {
            _latch.countDown();
            _conn = _client.connection();
            std::cout << "连接建立成功" << std::endl;
        }
        else
        {
            std::cout << "连接关闭" << std::endl;
        }
    }

    void defaultMessageCallback(const muduo::net::TcpConnectionPtr &conn, const MessagePtr &message, muduo::Timestamp)
    {
        LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
        conn->shutdown();
    }

    void Translate(std::string &str)
    {
        static const std::unordered_map<std::string, std::string> dict = {
            {"hello", "你好"},
            {"你好", "hello"},
            {"苹果", "apple"},
            {"apple", "苹果"}};

        if (dict.find(str) != dict.end())
        {
            str = dict.at(str);
        }
        else
        {
            str = "我不知道！";
        }
    }

    // 翻译处理函数收到的肯定是翻译请求
    void translate(const muduo::net::TcpConnectionPtr &conn, const ResponseTranslatePtr &message, muduo::Timestamp)
    {
        // 接收请求字段
        std::string msg = message->msg();
        std::cout << "翻译结果：" << msg << std::endl;
    }

    // 加法处理函数收到的肯定是翻译请求
    void add(const muduo::net::TcpConnectionPtr &conn, const ResponseAddPtr &message, muduo::Timestamp)
    {
        // 接收请求字段
        int result = message->result();
        std::cout << "运算结果：" << result << std::endl;
    }

private:

    ProtobufDispatcher _dispatcher;
    ProtobufCodec _codec;

    muduo::CountDownLatch _latch;

    muduo::net::EventLoopThread _loop;
    muduo::net::TcpClient _client;
    muduo::net::TcpConnectionPtr _conn;
};

int main()
{
    Client client;
    client.connect();

    client.addRequest(2, 3);
    client.translateRequest("hello");

    sleep(1);

    return 0;
}